package rds

import (
	"context"

	"github.com/redis/go-redis/v9"
)

type Config struct {
	Addr     string `json:"addr,omitempty"`
	DB       int    `json:"db,omitempty"`
	Username string `json:"username,omitempty"`
	Password string `json:"password,omitempty"`
}

var dbs = make(map[string]*redis.Client, 4)

func Init(configs map[string]Config) {
	for name, config := range configs {
		cli := redis.NewClient(&redis.Options{
			Addr:     config.Addr,
			DB:       config.DB,
			Username: config.Username,
			Password: config.Password,
		})

		err := cli.Ping(context.Background()).Err()
		if err != nil {
			panic(err)
		}

		dbs[name] = cli
	}
}

func DB(name string) *redis.Client {
	return dbs[name]
}
